package com.sgolub.commanndsonline.web;
   
import java.security.Principal;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.sgolub.commanndsonline.domain.entity.AdvertisingRegistry;
import com.sgolub.commanndsonline.domain.sec.SecUser;
import com.sgolub.commanndsonline.helpers.SecurityHelper;
import com.sgolub.commanndsonline.service.entity.AdvertisingRegistryService;
import com.sgolub.commanndsonline.service.sec.SecRoleService;
import com.sgolub.commanndsonline.service.sec.SecUserService;
 
@Controller
public class AppController { 
	 
    @Autowired
    private SecUserService secUserService;
    
    @Autowired
    private SecRoleService secRoleService;
    
    @Autowired
    private AdvertisingRegistryService advertisingRegistryService;
	
 
    @RequestMapping("/")
    public String home() {
        return "redirect:/index";
    }    
        
           
    @RequestMapping("/index") 
    public String resolveUserType(Map<String, Object> map, Principal principal) {
    	
    	System.out.println("AppController - resolveUserName = " + principal.getName());
    	System.out.println("AppController - resolveUserHigestRole = " + SecurityHelper.getUserHighestRole(principal.getName()));
    	 
    	if (SecurityHelper.getUserHighestRole(principal.getName()).equals("ROLE_ADMIN")) {
    		return "admin/adminMain";
    	} else if (SecurityHelper.getUserHighestRole(principal.getName()).equals("ROLE_USER")) {
    		return "user/userMain";
    	} else {
    		map.put("registration", new SecUser());
            return "registration";	
    	}  
    } 
       
    @RequestMapping("/registration")
    public String registration(Map<String, Object> map) {
    map.put("registration", new SecUser());
    System.out.println("AppController - registration");
    return "registration";
    }
     
     
    @RequestMapping("/admin/usersList") 
    public String reports(Map<String, Object> map) {
    System.out.println("AppController - usersList");
    map.put("user", 		new SecUser());
    map.put("usersList", 	secUserService.list());
    return "admin/usersList";
    }    
     

    @RequestMapping("/user/advertisingRegistryList") 
    public String advertisingRegistryList(Map<String, Object> map) {
    System.out.println("AppController - advertisingRegistryList");
     
    map.put("advertisingRegistry", 		new AdvertisingRegistry());
    map.put("advertisingRegistryList", 	advertisingRegistryService.list());
    return "user/advertisingRegistryList";
    }
    
//    @RequestMapping("/index2")
//    public String listContacts(Map<String, Object> map) {
//
//        map.put("contact", new Contact());
//        map.put("contactList", contactService.listContact());
//
//        return "contact";
//    } 
      
 

}